package com.admin.controller.declare;

import com.admin.common.enums.MemberLevelEnum;
import com.admin.common.shiro.ShiroUtils;
import com.admin.common.util.DateUtils;
import com.admin.common.util.YsCollectionUtil;
import com.admin.controller.base.BaseController;
import com.admin.req.declare.DeclareReq;
import com.admin.req.declare.DeclareSubmitReq;
import com.admin.service.DeclareService;
import com.admin.vo.PageVO;
import com.admin.vo.ResultVO;
import com.admin.vo.declare.DeclareListVO;
import com.admin.vo.declareDetail.DeclareDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 申报管理
 * 
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Api(description = "社区申报管理")
@RestController
@RequestMapping(value = "/declare")
public class DeclareController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DeclareController.class);

    @Autowired
    private DeclareService declareService;


    @ApiOperation(value = "社区申报管理-列表")
    @RequiresPermissions("declare:list")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "scoreStatus", value = "审批状态", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "declareType", value = "类型 0 正常 1 提标升级", dataType = "int", paramType = "query"),
    })
    public ResultVO<PageVO<DeclareListVO>> list(Integer pageIndex, Integer pageSize, Integer startTime, Integer endTime, Integer scoreStatus, Integer declareType) {
        Map<String, Object> params = YsCollectionUtil.createHashMap();
        params.put("startTime", DateUtils.dateFormatStr(startTime));
        params.put("endTime", DateUtils.dateFormatStr(endTime));
        params.put("scoreStatus", scoreStatus);
        params.put("declareType", declareType);
        params.put("orderBy", "id");
        return declareService.getPageList(pageIndex, pageSize, params);
    }


    @ApiOperation(value = "社区申报管理-列表 - 新增 or 修改")
    @RequiresPermissions("declare:add")
    @PostMapping(value = "/save")
    public ResultVO save(@RequestBody DeclareReq declareReq) {
        return declareService.save(ShiroUtils.getUserId(), declareReq);
    }

    @ApiOperation(value = "社区申报管理-列表 - 提交上级")
    @RequiresPermissions("declare:submit")
    @PostMapping(value = "/submitArea")
    public ResultVO<Boolean> submitArea(@RequestBody DeclareSubmitReq req) {
        return ResultVO.ok(declareService.submitArea(req.getId()));
    }

    @ApiOperation(value = "社区申报管理-列表 - 删除")
    @GetMapping(value = "/delete")
    @RequiresPermissions("declare:delete")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "int", paramType = "query")
    public ResultVO<Boolean> delete(Integer id) {
        return ResultVO.ok(declareService.delete(id));
    }

    @ApiOperation(value = "社区申报管理-列表 - 详情(预览)")
    @GetMapping(value = "/detail")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "int", paramType = "query")
    public ResultVO<DeclareDetailVO> detail(Integer id) {
        return ResultVO.ok(declareService.detail(id, MemberLevelEnum.USER.getKey()));
    }


    @ApiOperation(value = "社区申报管理-新增修改 - 评分规则查看")
    @GetMapping(value = "/scoreRule")
    @ApiImplicitParam(name = "moduleName", value = "子模块名称", required = true, dataType = "string", paramType = "query")
    public ResultVO<String> scoreRule(String moduleName) {
        return ResultVO.ok(declareService.getScoreRule(moduleName));
    }

}
